﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;

public class ABBuildWindows : EditorWindow
{

    private string mABName = string.Empty;
    private BuildTarget mPlateTarget = BuildTarget.StandaloneWindows;
    private BuildHelper.ObjectType mObjectType = BuildHelper.ObjectType.EPerfab;
    private BuildAssetBundleOptions mOptions = BuildAssetBundleOptions.StrictMode;



    public ABBuildWindows()
    {
        if (Application.platform == RuntimePlatform.WindowsEditor)
        {
            mPlateTarget = BuildTarget.StandaloneWindows;
        }
        else if (Application.platform == RuntimePlatform.Android)
        {
            mPlateTarget = BuildTarget.Android;
        }
        else if (Application.platform == RuntimePlatform.IPhonePlayer)
        {
            mPlateTarget = BuildTarget.iOS;
        }

    }


    public void OnGUI()
    {
        mABName = EditorGUILayout.TextField("AB包名称", mABName);
        mPlateTarget = (BuildTarget)EditorGUILayout.EnumPopup("平台选择", mPlateTarget);
        mObjectType = (BuildHelper.ObjectType)EditorGUILayout.EnumPopup("当前资源类型", mObjectType);
        mOptions = (BuildAssetBundleOptions)EditorGUILayout.EnumFlagsField("打包AB依赖选项", mOptions);

        BuildHelper.SetPath(mPlateTarget, EditorGUILayout.TextField("输出路径", BuildHelper.GetPath(mPlateTarget)));

        if (GUILayout.Button("开始打包"))
        {
            if (Selection.objects == null || Selection.objects.Length == 0)
                return;

            if (string.IsNullOrEmpty(mABName))
                return;

            BuildHelper.BuildAssetOneAB(mPlateTarget, mObjectType, mABName);
        }

        if (Selection.objects == null || Selection.objects.Length == 0)
        {
            EditorGUILayout.HelpBox("先选择你要打包的文件夹 或者 文件", MessageType.Info);
        }
        else
        {
            string str = string.Empty;
            string title = "已经选择文件: \n";
            str += title;
            Dictionary<string, string> titleList = new Dictionary<string, string>();

            Object[] objs = Selection.GetFiltered(typeof(Object), SelectionMode.DeepAssets);
            foreach (var item in objs)
            {

                string path = AssetDatabase.GetAssetPath(item);
                var type = AssetDatabase.GetMainAssetTypeAtPath(path);
                string typeStr = type.ToString();
                
                if (titleList.ContainsKey(typeStr))
                {
                    titleList[typeStr] += "\t" + item.name + "\n";
                }
                else
                {
                    titleList.Add(typeStr, "\t" + item.name + "\n");
                }


                //第一种格数
                //str += "\t" + item.name.PadRight(50) + type +  "\n";
            }

            foreach (var item in titleList)
            {
                str += item.Key + "\n";
                str += item.Value;
            }


            EditorGUILayout.HelpBox(str, MessageType.Info);
        }
    }

    [MenuItem("Build/AssetBundleEditor")]
    public static void OpenTestShipWinodw()
    {
        ABBuildWindows thisWindow = EditorWindow.GetWindow<ABBuildWindows>(false, "build", false);
    }

}
